package com.mz.mattress.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mz.mattress.admin.api.req.BlackQueryReq;
import com.mz.mattress.admin.api.req.OrderQueryReq;
import com.mz.mattress.admin.mapper.OrderDataMapper;
import com.mz.mattress.admin.mapper.OrderMapper;
import com.mz.mattress.admin.service.OrderService;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDataMapper orderDataMapper;

    @Override
    public Result<IPage<Order>> listPage(OrderQueryReq req) {
        Page page = req.newPage();
        IPage<Order> listPage = orderMapper.listPage(page,
                req.getMobile(), req.getStatus(), req.getOrderId(), req.getGoodsId(), req.getCoupon(), req.getStartTime(), req.getEndTime());

        List<Order> records = listPage.getRecords();
        if (records.isEmpty()) {
            return Result.ok(listPage);
        }

        Integer months = null;
        if (req.getStartTime() != null && req.getEndTime() != null) {
            if (req.getStartTime().equals("2025-03-01") && req.getEndTime().equals("2025-03-31")) {
                months = 3;
            } else if (req.getStartTime().equals("2025-04-01") && req.getEndTime().equals("2025-04-30")) {
                months = 4;
            } else if (req.getStartTime().equals("2025-05-01") && req.getEndTime().equals("2025-05-31")) {
                months = 5;
            } else if (req.getStartTime().equals("2025-06-01") && req.getEndTime().equals("2025-06-30")) {
                months = 6;
            } else if (req.getStartTime().equals("2025-07-01") && req.getEndTime().equals("2025-07-31")) {
                months = 7;
            }
        }

        Order times = records.get(0);

        if (months != null) {
            OrderData orderData = orderDataMapper.selectByMonth(months);

            SummaryTimes summaryTimes = new SummaryTimes();
            summaryTimes.setTimesCount(orderData.getOrderCount());
            summaryTimes.setUserCount(orderData.getPayCount());
            summaryTimes.setTotalPayMoney(orderData.getPayTotal());
            times.setSummaryTimes(summaryTimes);

        } else {
            SummaryTimes summaryTimes = orderMapper.listTotal(req.getMobile(), req.getStatus(), req.getOrderId(), req.getGoodsId(),
                    req.getCoupon(), req.getStartTime(), req.getEndTime());
            times.setSummaryTimes(summaryTimes);
        }

        return Result.ok(listPage);
    }

    @Override
    public Result<Order> getById(Integer id) {
        Order order = orderMapper.selectById(id);
        return Result.ok(order);
    }



//    @Override
//    public Result<List<OrderUseRecord>> selectByOrderId(Long orderId) {
//        List<OrderUseRecord> list = orderUseRecordMapper.list(orderId);
//        return Result.ok(list);
//    }

}
